<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
        v-for="(breadcrumb, index) in displayedBreadcrumbs"
        :key="breadcrumb.path"
      >
        <span v-if="isLastBreadcrumb(index)">{{ breadcrumb.meta.title }}</span>
        <a v-else @click.prevent="navigate(breadcrumb)">{{
          breadcrumb.meta.title
        }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import pathToRegexp from "path-to-regexp";

export default {
  name: "breadcrumbIndex",
  computed: {
    displayedBreadcrumbs() {
      return this.$route.matched
        .filter(
          (item) =>
            item.meta && item.meta.title && item.meta.breadcrumb !== false
        )
        .map((item) => ({ ...item, path: this.getPath(item.path) }));
    },
  },
  methods: {
    isLastBreadcrumb(index) {
      return index === this.displayedBreadcrumbs.length - 1;
    },
    getPath(path) {
      const keys = [];
      const compiledPath = pathToRegexp(path, keys);

      if (keys.length > 0) {
        return (params) => {
          return compiledPath.stringify(params);
        };
      }

      return path;
    },
    navigate(item) {
      const { redirect, path } = item;
      this.$router.push(redirect || { path });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    // This class is no longer used since we're handling it with computed properties
    // You can remove this or adjust your styling accordingly
  }
}
</style>
